package red.lsk.web.controller.business;



import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import red.lsk.business.domain.dto.ConsumeDto;
import red.lsk.business.domain.dto.RechargeDto;
import red.lsk.common.annotation.Log;
import red.lsk.common.enums.BusinessType;
import red.lsk.business.domain.BizMember;
import red.lsk.business.service.IBizMemberService;
import red.lsk.common.core.controller.BaseController;
import red.lsk.common.core.domain.AjaxResult;
import red.lsk.common.utils.poi.ExcelUtil;
import red.lsk.common.core.page.TableDataInfo;

/**
 * 会员管理Controller
 * 
 * @author ruoyi
 * @date 2024-05-09
 */
@Controller
@RequestMapping("/business/member")
public class BizMemberController extends BaseController
{
    private String prefix = "business/member";

    @Autowired
    private IBizMemberService bizMemberService;

    @RequiresPermissions("business:member:view")
    @GetMapping()
    public String member()
    {
        return prefix + "/member";
    }

    /**
     * 查询会员管理列表
     */
    @RequiresPermissions("business:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizMember bizMember)
    {
        startPage();
        List<BizMember> list = bizMemberService.selectBizMemberList(bizMember);
        return getDataTable(list);
    }

    /**
     * 导出会员管理列表
     */
    @RequiresPermissions("business:member:export")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizMember bizMember)
    {
        List<BizMember> list = bizMemberService.selectBizMemberList(bizMember);
        ExcelUtil<BizMember> util = new ExcelUtil<BizMember>(BizMember.class);
        return util.exportExcel(list, "会员管理数据");
    }

    /**
     * 新增会员管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会员管理
     */
    @RequiresPermissions("business:member:add")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizMember bizMember)
    {
        String phone = bizMember.getPhone();
        //首先判断手机号码是否为空,并且是否符合规范 用Hutools工具类
        if(StrUtil.isNotEmpty(phone)){
            if(!PhoneUtil.isMobile(phone)){
                return AjaxResult.error("手机号码不符合规范");
            }
        }else {
            return AjaxResult.error("手机号码不能为空");
        }
        //判断手机号码是否重复
        if(bizMemberService.countMemberByPhone(phone)>0){
            return AjaxResult.error("手机号码重复");
        }
        return toAjax(bizMemberService.insertBizMember(bizMember));
    }

    /**
     * 修改会员管理
     */
    @RequiresPermissions("business:member:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BizMember bizMember = bizMemberService.selectBizMemberById(id);
        mmap.put("bizMember", bizMember);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员管理
     */
    @RequiresPermissions("business:member:edit")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizMember bizMember)
    {
        return toAjax(bizMemberService.updateBizMember(bizMember));
    }

    /**
     * 删除会员管理
     */
    @RequiresPermissions("business:member:remove")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizMemberService.deleteBizMemberByIds(ids));
    }

    /**
     * 修改会员管理
     */
    @RequiresPermissions("business:member:edit")
    @GetMapping("/recharge/{id}")
    public String recharge(@PathVariable("id") String id, ModelMap mmap)
    {
        System.out.println("id:"+id);
        BizMember bizMember = bizMemberService.selectBizMemberById(id);
        mmap.put("bizMember", bizMember);
        return prefix + "/recharge";
    }

    /**
     * 充值 recharge
     */
    @RequiresPermissions("business:member:edit")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/recharge")
    @ResponseBody
    public AjaxResult rechargeSave(RechargeDto rechargeDto)
    {
        BigDecimal money = rechargeDto.getMoney();
        //String phone = rechargeDto.getPhone();
        String memberId = rechargeDto.getMemberId();
        String remark = rechargeDto.getRemark();

        //需要接收到充值金额 和 会员手机号码
        //判断手机号码和充值金额是否为空
        if(money==null) {
            return AjaxResult.error("充值金额不能为空");
        }
        if(StrUtil.isEmpty(memberId)){
            return AjaxResult.error("会员编号不能为空");
        }
        //判断money是否大于0.01
        if(money.compareTo(new BigDecimal("0.01")) <= 0){
           // throw new RuntimeException("充值金额必须大于0");
            return AjaxResult.error("充值金额必须大于0.01元");
        }
        int rows = bizMemberService.rechargeMemberByMemberId(memberId, money, remark);
        return toAjax(rows);
    }

    /**
     * 消费
     */
    @RequiresPermissions("business:member:edit")
    @GetMapping("/consume/{id}")
    public String consume(@PathVariable("id") String id, ModelMap mmap)
    {
        BizMember bizMember = bizMemberService.selectBizMemberById(id);
        mmap.put("bizMember", bizMember);
        //BigDecimal discountRatio = bizMember.getDiscountRatio();
        return prefix + "/consume";
    }

    /**
     * 消费
     *
     */
    @RequiresPermissions("business:member:edit")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/consume")
    @ResponseBody
    public AjaxResult consumeSave(ConsumeDto consumeDto)
    {
        BigDecimal money = consumeDto.getMoney();
        String memberId = consumeDto.getMemberId();
        //String remark = consumeDto.getRemark();

        BigDecimal discountRatio = consumeDto.getDiscountRatio();
        //判断范围是否在1-100
        if(discountRatio.compareTo(new BigDecimal("1")) < 0 || discountRatio.compareTo(new BigDecimal("100")) > 0){
            return AjaxResult.error("折扣范围在1-100");
        }
        //判断手机号码和消费金额是否为空
        if(money==null) {
            return AjaxResult.error("消费金额不能为空");
        }
        if(StrUtil.isEmpty(memberId)){
            return AjaxResult.error("会员编号不能为空");
        }
        //判断money是否大于0.01
        if(money.compareTo(new BigDecimal("0.01")) <= 0){
            // throw new RuntimeException("充值金额必须大于0");
            return AjaxResult.error("消费金额必须大于0.01");
        }
        int rows = bizMemberService.consumeByMemberId(consumeDto);
        return toAjax(rows);

    }

}
